import java.util.*;
import java.io.*;

public class Client {
	
	public ArrayList<String> servers;
    public Client() {
    	this.servers = new ArrayList<String>();
    	this.citire();
    }
    
    public void citire(){
    	//citire din fisierul de configurare config.txt
		try{
		  	// Open the file that is the first 
		  	// command line parameter
		  	FileInputStream fstream = new FileInputStream("config.txt");
		  	// Get the object of DataInputStream
		  	DataInputStream in = new DataInputStream(fstream);
		  	BufferedReader br = new BufferedReader(new InputStreamReader(in));
		  	String strLine;
		  	//Read File Line By Line
		 	while ((strLine = br.readLine()) != null)   {
		  	// Print the content on the console
		  	StringTokenizer parser = new StringTokenizer(strLine);
				while (parser.hasMoreTokens()) {
			    	this.servers.add(parser.nextToken());
				}
			}
		  	//Close the input stream
		  	in.close();
		}catch (Exception e){//Catch exception if any
		  	System.err.println("Error: " + e.getMessage());
		}

    }
    
    public String get_comanda() throws IOException{
    	String CurLine = ""; // Line read from standard in
    	System.out.println("Enter a line of text (type 'quit' to exit): ");
    	InputStreamReader converter = new InputStreamReader(System.in);
    	BufferedReader in = new BufferedReader(converter);
    	CurLine = in.readLine();
    	return CurLine;
    }
    
    public void parse_comanda(String comanda){
    	StringTokenizer parser = new StringTokenizer(comanda);
		while (parser.hasMoreTokens()) {
	    	this.servers.add(parser.nextToken());
		}
    }
    
    public static void main(String args[]) throws IOException {
		
		Client cli = new Client();
		String comanda, word;
		StringTokenizer parser;
		int i;
		
		/*
		for(i=0;i<cli.servers.size();i++)
			System.out.println(cli.servers.get(i));
		*/
		
		for(i=0;i<cli.servers.size()/3;i++){
			if(cli.servers.get(3*i+2).equals("TCP")){
				//conectare server TCP
				System.out.println("conectare server TCP");
				System.out.println(cli.servers.get(3*i) + " " + cli.servers.get(3*i+1));
				
				//TCPClient client = new TCPClient(cli.servers.get(3*i), Integer.parseInt(cli.servers.get(3*i+1)));
				TCPClient client = new TCPClient("192.168.15.171", 51256);
				
				while(!(comanda=cli.get_comanda()).equals("quit")){
					
					parser = new StringTokenizer(comanda);
					if(!parser.hasMoreTokens())
						continue;
					else
						word=parser.nextToken();
					
					if(word.equals("put")){
						if(!parser.hasMoreTokens()){
							System.out.println("Comanda put incompleta, reintroduceti comanda");
							continue;
						}
						else{
							word=parser.nextToken();
							client.caleFisier=word;
							client.operatie=1;
						}
					}
					else if(word.equals("get")){
						if(!parser.hasMoreTokens()){
							System.out.println("Comanda get incompleta, reintroduceti comanda");
							continue;
						}
						else{
							word=parser.nextToken();
							client.idFisier=word;
							client.operatie=2;
						}
						if(!parser.hasMoreTokens()){
							System.out.println("Comanda get 'ID' incompleta, reintroduceti comanda");
							continue;
						}
						else{
							word=parser.nextToken();
							client.caleFisier=word;
						}
					}
					else if(word.equals("filter")){
						if(!parser.hasMoreTokens()){
							System.out.println("Comanda filter incompleta, reintroduceti comanda");
							continue;
						}
						else{
							word=parser.nextToken();
							client.idFisier=word;
							client.operatie=3;
						}
						if(!parser.hasMoreTokens()){
							System.out.println("Comanda filter 'numeFiltru' incompleta, reintroduceti comanda");
							continue;
						}
						else{
							word=parser.nextToken();
							client.caleFisier=word;
						}
					}
					else if(word.equals("resize")){
						if(!parser.hasMoreTokens()){
							System.out.println("Comanda resize incompleta, reintroduceti comanda");
							continue;
						}
						else{
							word=parser.nextToken();
							client.lungime=Integer.parseInt(word);
							client.operatie=4;
						}
						if(!parser.hasMoreTokens()){
							System.out.println("Comanda resize incompleta, reintroduceti comanda");
							continue;
						}
						else{
							word=parser.nextToken();
							client.inaltime=Integer.parseInt(word);
						}
						if(!parser.hasMoreTokens()){
							System.out.println("Comanda resize incompleta, reintroduceti comanda");
							continue;
						}
						else{
							word=parser.nextToken();
							client.idFisier=word;
						}
					}
					else {
						System.out.println("Comanda gresita, reintroduceti comanda");
						continue;
					}
					System.out.println("start TCP");
					client.start();
					try {
						client.join();
					} catch (Exception e) {}
					
				}
				break;
			}
			else if(cli.servers.get(3*i+2).equals("UDP")){
				//conectare server UDP
				System.out.println("conectare server UDP");
				System.out.println(cli.servers.get(3*i) + " " + cli.servers.get(3*i+1));
				
				//UDPClient client = new UDPClient(cli.servers.get(3*i), Integer.parseInt(cli.servers.get(3*i+1)), 60000);
							
				while(!(comanda=cli.get_comanda()).equals("quit")){
					UDPClient client = new UDPClient("192.168.15.171", 51257, 44881);
					
					parser = new StringTokenizer(comanda);
					if(!parser.hasMoreTokens())
						continue;
					else
						word=parser.nextToken();
					if(word.equals("put")){
						if(!parser.hasMoreTokens()){
							System.out.println("Comanda put incompleta, reintroduceti comanda");
							continue;
						}
						else{
							word=parser.nextToken();
							client.caleFisier=word;
							client.operatie=1;
						}
					}
					else if(word.equals("get")){
						if(!parser.hasMoreTokens()){
							System.out.println("Comanda get incompleta, reintroduceti comanda");
							continue;
						}
						else{
							word=parser.nextToken();
							client.idFisier=word;
							client.operatie=2;
						}
						if(!parser.hasMoreTokens()){
							System.out.println("Comanda get 'ID' incompleta, reintroduceti comanda");
							continue;
						}
						else{
							word=parser.nextToken();
							client.caleFisier=word;
						}
					}
					else if(word.equals("filter")){
						if(!parser.hasMoreTokens()){
							System.out.println("Comanda filter incompleta, reintroduceti comanda");
							continue;
						}
						else{
							word=parser.nextToken();
							if(word.isEmpty())
							{
								word=parser.nextToken();
							}
							client.numeFiltru=word;
							client.operatie=3;
						}
						if(!parser.hasMoreTokens()){
							System.out.println("Comanda filter 'numeFiltru' incompleta, reintroduceti comanda");
							continue;
						}
						else{
							word=parser.nextToken();
							client.idFisier=word;
						}
					}
					else if(word.equals("resize")){
						if(!parser.hasMoreTokens()){
							System.out.println("Comanda resize incompleta, reintroduceti comanda");
							continue;
						}
						else{
							word=parser.nextToken();
							client.lungime=Integer.parseInt(word);
							client.operatie=4;
						}
						if(!parser.hasMoreTokens()){
							System.out.println("Comanda resize incompleta, reintroduceti comanda");
							continue;
						}
						else{
							word=parser.nextToken();
							client.inaltime=Integer.parseInt(word);
						}
						if(!parser.hasMoreTokens()){
							System.out.println("Comanda resize incompleta, reintroduceti comanda");
							continue;
						}
						else{
							word=parser.nextToken();
							client.idFisier=word;
						}
					}
					else {
						System.out.println("Comanda gresita, reintroduceti comanda");
						continue;
					}
				
					System.out.println("start UDP");
					client.start();
					try {
						client.join();
					} catch (Exception e) {}
				}
				break;
			}
			else if(cli.servers.get(3*i+2).equals("NIO")){
				//conectare server NIO
				System.out.println("conectare server NIO");
				break;
			}	
		}
	}
}